package cn.iocoder.yudao.module.member.dal.mysql.email;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.member.dal.dataobject.email.EmailDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.member.controller.admin.email.vo.*;

/**
 * 用户邮箱 Mapper
 *
 * @author 超管
 */
@Mapper
public interface EmailMapper extends BaseMapperX<EmailDO> {

    default PageResult<EmailDO> selectPage(EmailPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<EmailDO>()
                .eqIfPresent(EmailDO::getUserId, reqVO.getUserId())
                .eqIfPresent(EmailDO::getEmail, reqVO.getEmail())
                .betweenIfPresent(EmailDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(EmailDO::getId));
    }

    default EmailDO getByEmail(Long userId, String email) {
        return selectOne(new LambdaQueryWrapperX<EmailDO>().eq(EmailDO::getUserId, userId).eq(EmailDO::getEmail, email));
    }

    default void updateUsageCount(Long id) {
        update(new LambdaUpdateWrapper<EmailDO>().setSql("usage_count = usage_count + 1").eq(EmailDO::getId, id));
    }

    default List<EmailDO> getEmailOrderByUpdateTime(Long userId, int limitSize) {
        return selectList(new LambdaQueryWrapperX<EmailDO>().eq(EmailDO::getUserId, userId)
                .orderByDesc(EmailDO::getUpdateTime)
                .last("limit " + limitSize));
    }

}